﻿// =================================================================== 
// 项目说明
//====================================================================
// sushee。@Copy Right 2019
// 文件： SmsTask.cs
// 项目名称：斑马车务
// 创建时间：2019/9/19
// 负责人：sushee
// ===================================================================
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Dapper.Extensions;
using YouWei.Infrastructure.Entity;
using YouWei.Infrastructure.Repository;
using YouWei.Infrastructure.Models;

namespace YouWei.Infrastructure.Service
{
	
	/// <summary>
	///短信任务
	/// </summary>
	public class SmsTaskService
	{
         protected SmsTaskRepository repository;
         
         public  SmsTaskService(){
           repository = new SmsTaskRepository("CarServerConnectionString");         
         }
         
        #region 增删改查

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public object Insert(SmsTask entity) { return repository.Insert(entity); }

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public object Insert(List<SmsTask> entitys) { return repository.Insert(entitys); }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public bool Update(SmsTask entity) { return repository.Update(entity); }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public bool Delete(SmsTask entity) { return repository.Delete(entity); }

        /// <summary>
        /// 删除所有
        /// </summary>
        /// <returns></returns>
        public bool DeleteAll() { return repository.DeleteAll(); }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="primaryKey">主键</param>
        /// <returns></returns>
        public bool Delete(int primaryKey) { return repository.Delete(primaryKey); }

        /// <summary>
        /// 查询单个实体
        /// </summary>
        /// <param name="primaryKey">主键</param>
        /// <returns></returns>
        public SmsTask SingleOrDefault(object primaryKey) { return repository.SingleOrDefault(primaryKey); }

       
        /// <summary>
        /// 查询单个实体,如果存在多条则引发异常
        /// </summary>
        /// <param name="primaryKey">主键</param>
        /// <returns></returns>
        public SmsTask Single(object primaryKey) { return repository.Single(primaryKey); }

        /// <summary>
        /// 查询所有
        /// </summary>
        /// <returns></returns>
        public List<SmsTask> QueryAll()
        {
            return repository.QueryAll();
        }

        #endregion

        #region 单表查询帮助方法
        /// <summary>
        /// 根据谓词表达式获取一个对象，如果没有获取到对象，则返回null
        /// </summary>
        /// <param name="predicate">谓词表达式</param>
        /// <param name="useCache">是否使用缓存</param>
        /// <returns></returns>
        public SmsTask QuerySingle(Expression<Func<SmsTask, bool>> predicate, bool useCache = false)
        {
            return repository.QuerySingle(predicate);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="M"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="useCache"></param>
        /// <returns></returns>
        public M QuerySingle<M>(Expression<Func<M, bool>> predicate, bool useCache = false) where M : class
        {
            return repository.QuerySingle<M>(predicate, useCache);
        }
        /// <summary>
        /// 根据谓词表达式获取一个对象列表，如果没有获取到对象，则返回一个空列表
        /// </summary>
        /// <param name="predicate">谓词表达式</param>
        /// <param name="useCache">是否使用缓存</param>
        /// <returns></returns>
        public List<SmsTask> QueryList(Expression<Func<SmsTask, bool>> predicate, bool useCache = false)
        {
            return repository.QueryList(predicate, useCache);
        }
        /// <summary>
        /// 根据谓词表达式获取一个对象列表，如果没有获取到对象，则返回一个空列表
        /// </summary>
        /// <typeparam name="M"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="useCache"></param>
        /// <returns></returns>
        public List<M> QueryList<M>(Expression<Func<M, bool>> predicate, bool useCache = false) where M : class
        {
            return repository.QueryList<M>(predicate, useCache);
        }

        #endregion

        #region 业务逻辑
        /// <summary>
        /// 根据指定时间获取可用的任务
        /// </summary>
        /// <param name="dtTime"></param>
        /// <returns></returns>
	    public List<SmsTask> GetAllTasks(DateTime dtTime)
	    {
	        return repository.GetAllTasks(dtTime);
	    }
        /// <summary>
        /// 根据任务ID获取任务周期
        /// </summary>
        /// <param name="taskID"></param>
        /// <returns></returns>
	    public List<SmsTaskTime> GetAllTaskTimes(SmsTask task)
	    {
	        var list = repository.QueryList<SmsTaskTime>(m => m.TaskID == task.TaskID && m.IsDeleted==0);
            var dtNow = DateTime.Now;
            list=list.Where(m => !CheckTaskTimeExpired(task,dtNow, m)).ToList();

            return list;
	    }
        private bool CheckTaskTimeExpired(SmsTask task,DateTime dtNow, SmsTaskTime taskTime)
        {
            bool bReturn = false;
            if (taskTime.TimeType == 1)
            {
                if (task.StartDate <=dtNow && dtNow< task.EndDate  && dtNow.Hour >= taskTime.StartTime && dtNow.Hour < taskTime.EndTime)
                {

                }
                else
                {
                    bReturn = true;
                }
            }
            return bReturn;

        }
        /// <summary>
        /// 根据任务ID获取任务的黑名单日期
        /// </summary>
        /// <param name="taskID"></param>
        /// <returns></returns>
        public List<SmsBlackDay> GetAllTaskBlackDay(int taskID)
        {
            var list = repository.QueryList<SmsBlackDay>(m => m.TaskID == taskID);
            return list;
        }
        /// <summary>
        /// 根据任务获取一条需要发送的短信
        /// </summary>
        /// <param name="task"></param>
        /// <returns></returns>
	    public SmsMessagePool GetAnToSendMsg(SmsTask task)
	    {
	        var oReturn =
	            repository.QuerySingle<SmsMessagePool>(m => m.CarBrand == task.CarBrand && task.CarSerial == m.CarSerial && m.IsJoinSendQueue==0);
	        return oReturn;
	    }

        /// <summary>
        /// 分页查询，带条件
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="condition"></param>
        /// <returns></returns>
        public Page<SmsTaskDto> GetPages(int pageIndex, long pageSize, ReqSmsTaskCondition condition)
        {
            return repository.GetPages(pageIndex, pageSize, condition);
        }

        #endregion




    }
	
}